/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007-2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.spring.transaction.bean.impl;

import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;

import com.genia.toolbox.basics.exception.technical.TechnicalException;
import com.genia.toolbox.spring.transaction.bean.EnlistableResource;

/**
 * an {@link EnlistableResource} that allow transactional access of a simple
 * immutable object.
 * 
 * @param <TYPE>
 *          the type of the object.
 */
public class EnlistableResourceImmutableValue<TYPE>
    implements EnlistableResource
{

  /**
   * the base {@link AbstractEnlistableResource} that provide transactional
   * comportment.
   */
  private class EnlistableResourceImmutableValueObject
      extends AbstractEnlistableResource<TYPE>
  {

    /**
     * clone the originalObject to generate a transactional view of it.
     * 
     * @param originalObject
     *          the object to clone
     * @return a clone of the originalObject.
     * @see com.genia.toolbox.spring.transaction.bean.impl.AbstractEnlistableResource#cloneObjectForTransaction(java.lang.Object)
     */
    @Override
    public TYPE cloneObjectForTransaction(TYPE originalObject)
    {
      return originalObject;
    }

  }

  /**
   * the delegate object for transactional comportment.
   */
  private transient final EnlistableResourceImmutableValueObject backingObject = new EnlistableResourceImmutableValueObject();



  /**
   * returns the transactional object in its current state.
   * 
   * @return the transactional object in its current state
   */
  @Transactional(readOnly = true)
  public TYPE getObject()
  {
    return backingObject.getTransactionalView().getObjectForReading();
  }



  /**
   * set the transactional object in its current state.
   * 
   * @param object
   *          the value the object must have in its current state
   */
  @Transactional
  public void setObject(TYPE object)
  {
    backingObject.getTransactionalView().setObject(object);
  }



  /**
   * commit the transaction defined by {@link TransactionStatus}. A
   * {@link #prepare(TransactionStatus)} must have been called.
   * 
   * @param status
   *          the {@link TransactionStatus} of the transaction
   * @throws TechnicalException
   *           if an error occured
   * @see com.genia.toolbox.spring.transaction.bean.EnlistableResource#commit(org.springframework.transaction.TransactionStatus)
   */
  public void commit(TransactionStatus status)
      throws TechnicalException
  {
    backingObject.commit(status);
  }



  /**
   * prepare the transaction defined by {@link TransactionStatus}. If this
   * method succeed, a {@link #commit(TransactionStatus)} must succeed or the
   * transactional integrity of the application won't be insured.
   * 
   * @param status
   *          the {@link TransactionStatus} of the transaction
   * @throws TechnicalException
   *           if an error occured
   * @see com.genia.toolbox.spring.transaction.bean.EnlistableResource#prepare(org.springframework.transaction.TransactionStatus)
   */
  public void prepare(TransactionStatus status)
      throws TechnicalException
  {
    backingObject.prepare(status);
  }



  /**
   * rollback the transaction defined by {@link TransactionStatus}.
   * 
   * @param status
   *          the {@link TransactionStatus} of the transaction
   * @throws TechnicalException
   *           if an error occured
   * @see com.genia.toolbox.spring.transaction.bean.EnlistableResource#rollback(org.springframework.transaction.TransactionStatus)
   */
  public void rollback(TransactionStatus status)
      throws TechnicalException
  {
    backingObject.rollback(status);
  }



  /**
   * start a new transaction for the current object.
   * 
   * @param definition
   *          {@link TransactionDefinition} instance (can be <code>null</code>
   *          for defaults), describing propagation behavior, isolation level,
   *          timeout etc
   * @param status
   *          the {@link TransactionStatus} of the transaction
   * @throws TechnicalException
   *           if an error occured
   * @see com.genia.toolbox.spring.transaction.bean.EnlistableResource#startTransaction(org.springframework.transaction.TransactionDefinition,
   *      org.springframework.transaction.TransactionStatus)
   */
  public void startTransaction(TransactionDefinition definition, TransactionStatus status)
      throws TechnicalException
  {
    backingObject.startTransaction(definition, status);
  }
}
